﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;


public partial class AccountManagement : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Form.DefaultFocus = idBx.ClientID;
        Page.Form.DefaultButton = regBtn.UniqueID;

        idBx.Enabled = false;
        passwdBx.Enabled = false;
        alreadyInBtn.Enabled = false;

        if (!IsPostBack)
        {
            btnList.Enabled = false;

            if (Session["userID"] == null)
            {
                updateBtn.Enabled = false;
                idBx.Enabled = true;
                passwdBx.Enabled = true;
                btnList.Enabled = true;
                alreadyInBtn.Enabled = true;
            }
            else
            {
                alreadyInBtn.Visible = false;
                regBtn.Visible = false;
                OleDbConnection conn = null;
                OleDbDataReader reader = null;
                string id = (string)Session["userID"];
                string type;

                try
                {
                    conn = new OleDbConnection(
                        "Provider=Microsoft.ACE.OLEDB.12.0; " +
                        "Data Source=" + Server.MapPath("App_Data/BayouFarmersDB.accdb"));
                    conn.Open();

                    OleDbCommand cmd = new OleDbCommand("Select * FROM Users WHERE ID='" + id + "'", conn);
                    reader = cmd.ExecuteReader();

                    while (reader.Read())
                    {
                        idBx.Text = (string)reader["ID"];
                        passwdBx.Text = (string)reader["password"];
                        firstNameBx.Text = (string)reader["firstName"];
                        lastNameBx.Text = (string)reader["lastName"];
                        emailBx.Text = (string)reader["email"];
                        addressBx.Text = reader["addressNumber"].ToString();
                        streetBx.Text = (string)reader["street"];
                        cityBx.Text = (string)reader["city"];
                        stateBx.Text = (string)reader["state"];
                        zipBx.Text = reader["zip"].ToString();
                        countryBx.Text = (string)reader["country"];
                        phoneBx.Text = reader["phone"].ToString();

                        type = (string)reader["type"];
                        if (type == "B")
                        { btnList.SelectedIndex = 0; }
                        if (type == "S")
                        { btnList.SelectedIndex = 1; }
                    }
                }
                finally
                {
                    regBtn.Enabled = false;
                    if (reader != null) reader.Close();
                    if (conn != null) conn.Close();
                }
            }
        }
    }

    protected void regBtn_Click(object sender, EventArgs e)
    {
        OleDbConnection conn = null;
        OleDbDataReader reader = null;
        int trigger = 0;

        
            string bORs = null;

            if (btnList.SelectedIndex == 0)
            { bORs = "B"; }
            if (btnList.SelectedIndex == 1)
            { bORs = "S"; }

            conn = new OleDbConnection(
                "Provider=Microsoft.ACE.OLEDB.12.0; " +
                "Data Source=" + Server.MapPath("App_Data/BayouFarmersDB.accdb"));
            conn.Open();

            OleDbCommand readcmd = new OleDbCommand("Select * FROM Users", conn);
            reader = readcmd.ExecuteReader();

            while (reader.Read())
            {
                if (idBx.Text == (string)reader["ID"])
                {
                    trigger++;
                }

            }

            if (trigger > 0)
            {
                string ScriptString = "alert('This user ID is already in use!');";
                ScriptString += "window.location='Default.aspx';";
                System.Web.UI.Page page = HttpContext.Current.Handler as System.Web.UI.Page;
                page.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "ClientScript", ScriptString, true);

                if (reader != null) reader.Close();
                if (conn != null) conn.Close();
            }
            else
            {
                try
                {
                    string insertQuery;
                    insertQuery = "INSERT INTO Users ";
                    insertQuery += "Values ('" + idBx.Text + "','" + firstNameBx.Text + "','" + lastNameBx.Text + "','" + passwdBx.Text + "'";
                    insertQuery += ",'" + addressBx.Text + "','" + streetBx.Text + "','" + cityBx.Text + "','" + stateBx.Text + "'";
                    insertQuery += ",'" + countryBx.Text + "','" + zipBx.Text + "','" + phoneBx.Text + "','" + emailBx.Text + "','" + bORs + "')";

                    OleDbCommand cmd = new OleDbCommand(insertQuery, conn);
                    cmd.ExecuteNonQuery();

                    string ScriptString = "alert('Successfully created the account!');";
                    ScriptString += "window.location='Default.aspx';";
                    System.Web.UI.Page page = HttpContext.Current.Handler as System.Web.UI.Page;
                    page.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "ClientScript", ScriptString, true);
                }
                finally
                {
                    if (reader != null) reader.Close();
                    if (conn != null) conn.Close();
                }
            }
    }
    protected void backBtn_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Default.aspx");
    }
    protected void createAuctionBtn_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/CreateAuction.aspx");
    }

    protected void updateBtn_Click(object sender, EventArgs e)
    {
        OleDbConnection conn = null;

        try
        {
            string id = (string)Session["userID"];

            conn = new OleDbConnection(
                "Provider=Microsoft.ACE.OLEDB.12.0; " +
                "Data Source=" + Server.MapPath("App_Data/BayouFarmersDB.accdb"));

            string updateCmd;

            updateCmd = "UPDATE Users SET firstName='" + firstNameBx.Text + "', lastName='" + lastNameBx.Text + "', addressNumber='" + addressBx.Text + "', ";
            updateCmd += "street='" + streetBx.Text + "', city='" + cityBx.Text + "', state='" + stateBx.Text + "', country='" + countryBx.Text + "', ";
            updateCmd += "zip='" + zipBx.Text + "', phone='" + phoneBx.Text + "', email='" + emailBx.Text + "' ";
            updateCmd += "WHERE ID='" + id + "'";

            OleDbCommand cmd = new OleDbCommand(updateCmd, conn);

            conn.Open();
            cmd.ExecuteNonQuery();            
        }
        finally
        {
            conn.Close();
            string ScriptString = "alert('Updates successful!');";
            ScriptString += "window.location='Default.aspx';";
            System.Web.UI.Page page = HttpContext.Current.Handler as System.Web.UI.Page;
            page.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "ClientScript", ScriptString, true);            
        }
    }
    protected void alreadyInBtn_Click(object sender, EventArgs e)
    {
        OleDbConnection conn = null;
        OleDbDataReader reader = null;
        int trigger = 0;

        try
        {
            conn = new OleDbConnection(
                "Provider=Microsoft.ACE.OLEDB.12.0; " +
                "Data Source=" + Server.MapPath("App_Data/BayouFarmersDB.accdb"));
            conn.Open();

            OleDbCommand cmd = new OleDbCommand("Select * FROM Users", conn);
            reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                if (idBx.Text == (string)reader["ID"])
                {
                    trigger++;
                }
                
            }
        }
        finally
        {
            if (trigger > 0)
            {
                string ScriptString = "alert('This user ID is already in use!');";
                ScriptString += "window.location='Default.aspx';";
                System.Web.UI.Page page = HttpContext.Current.Handler as System.Web.UI.Page;
                page.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "ClientScript", ScriptString, true);
            }
            else
            {
                string ScriptString = "alert('This user ID is not currently in use!');";
                ScriptString += "window.location='Default.aspx';";
                System.Web.UI.Page page = HttpContext.Current.Handler as System.Web.UI.Page;
                page.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "ClientScript", ScriptString, true);
            }

            if (reader != null) reader.Close();
            if (conn != null) conn.Close();
        }
    }
    
}